package com.zx.BookSeaReader.ui.home.bookcommunity;

import android.content.Intent;
import android.os.Handler;
import android.widget.ArrayAdapter;

import com.zx.BookSeaReader.R;
import com.zx.BookSeaReader.base.BasePresenter;
import com.zx.BookSeaReader.common.APPCONST;
import com.zx.BookSeaReader.greendao.entity.BookCommentBean;
import com.zx.BookSeaReader.greendao.entity.BookListBean;
import com.zx.BookSeaReader.greendao.entity.UserBean;
import com.zx.BookSeaReader.greendao.service.BookListService;
import com.zx.BookSeaReader.ui.booklist.BookListActivity;
import com.zx.BookSeaReader.ui.booklistcreate.BookListCreateActivity;
import com.zx.BookSeaReader.ui.booklistdetail.BookListDetailActivity;
import com.zx.BookSeaReader.util.TextHelper;
import com.zx.BookSeaReader.widget.itemdecoration.DividerItemDecoration;
import com.zx.BookSeaReader.widget.itemdecoration.FullyLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;

/**
 * @Author: ZX
 * @Time: 2020/4/20
 * @Description: 功能描述
 */
public class BookCommunityPresenter  implements BasePresenter {

    private UserBean userBean;
    private BookListBean mBookListBean;
    private BookCommunityFragment mBookCommunityFragment;


    private BookListService mBookListService;
    private List<BookListBean> mybookLists = new ArrayList<>();
    private List<BookListBean> subbookLists = new ArrayList<>();



    private BookListSubscribeAdapter mBookListSubscribeAdapter;
    private BookListCreateAdapter mBookListCreateAdapter;
    //private List<Book> bookList;

    private Handler mHandle = new Handler(message -> {
        switch (message.what){
            case 1:
                mBookListService.deleteBookListSub(mBookListBean,userBean);
                TextHelper.showText("删除成功");
                initAdapter();
                break;

        }
        return false;
    });






    BookCommunityPresenter(BookCommunityFragment bookCommunityFragment){
        mBookCommunityFragment = bookCommunityFragment;
        mBookListService = new BookListService();
        mBookListCreateAdapter= new BookListCreateAdapter();
        mBookListSubscribeAdapter= new BookListSubscribeAdapter();

    }




    @Override
    public void start() {
        userBean = (UserBean)mBookCommunityFragment.getActivity().getIntent().getSerializableExtra(APPCONST.USER);
        mBookCommunityFragment.getRvCreateList().setLayoutManager(new FullyLinearLayoutManager(mBookCommunityFragment.getActivity()));
        mBookCommunityFragment.getRvCreateList().addItemDecoration(new DividerItemDecoration(mBookCommunityFragment.getActivity()));
        mBookCommunityFragment.getRvCreateList().setAdapter(mBookListCreateAdapter);

        mBookCommunityFragment.getRvBookList().setLayoutManager(new FullyLinearLayoutManager(mBookCommunityFragment.getActivity()));
        mBookCommunityFragment.getRvBookList().addItemDecoration(new DividerItemDecoration(mBookCommunityFragment.getActivity()));
        mBookCommunityFragment.getRvBookList().setAdapter(mBookListSubscribeAdapter);

        mBookListCreateAdapter.setOnItemClickListener(
                (v, pos) -> {
                    Intent intent = new Intent(mBookCommunityFragment.getActivity(), BookListDetailActivity.class);
                    intent.putExtra(APPCONST.BOOKLIST, mBookListCreateAdapter.getItem(pos));
                    intent.putExtra(APPCONST.USER, userBean);
                    mBookCommunityFragment.getActivity().startActivity(intent);

                }
        );
        mBookListSubscribeAdapter.setOnItemClickListener(
                (v, pos) -> {
                    Intent intent = new Intent(mBookCommunityFragment.getActivity(), BookListDetailActivity.class);
                    intent.putExtra(APPCONST.BOOKLIST, mBookListSubscribeAdapter.getItem(pos));
                    intent.putExtra(APPCONST.USER, userBean);
                    mBookCommunityFragment.getActivity().startActivity(intent);

                }
        );

        mBookListCreateAdapter.setOnItemLongClickListener(
                (v, pos) -> {
                    openMyListItemDialog(mBookListCreateAdapter.getItem(pos));
                    return true;
                }
        );
        mBookListSubscribeAdapter.setOnItemLongClickListener(
                (v, pos) -> {
                    openListItemDialog(mBookListSubscribeAdapter.getItem(pos));
                    return true;
                }
        );


        mBookCommunityFragment.getTvCreateBookList().setOnClickListener(
                (view) -> {
                    Intent intent = new Intent(mBookCommunityFragment.getActivity(), BookListCreateActivity.class);
                    intent.putExtra(APPCONST.USER, userBean);
                    mBookCommunityFragment.getActivity().startActivity(intent);
                }
        );

        mBookCommunityFragment.getTvAddBookList().setOnClickListener(
                (view) -> {
                    Intent intent = new Intent(mBookCommunityFragment.getActivity(), BookListActivity.class);
                    intent.putExtra(APPCONST.USER, userBean);
                    mBookCommunityFragment.getActivity().startActivity(intent);
                }
        );

    }

    private void openMyListItemDialog(BookListBean bookListBean) {
        String[] menus;
        menus = mBookCommunityFragment.getResources().getStringArray(R.array.menu_long_my_list_click);
        AlertDialog commentBeanDialog = new AlertDialog.Builder( mBookCommunityFragment.getContext())
                .setTitle(bookListBean.getTitle())
                .setAdapter(new ArrayAdapter<String>( mBookCommunityFragment.getContext(),
                                android.R.layout.simple_list_item_1, menus),
                        (dialog, which) -> onMyListItemMenuClick(menus[which], bookListBean))
                .setNegativeButton(null, null)
                .setPositiveButton(null, null)
                .create();

        commentBeanDialog.show();
    }


    private void openListItemDialog(BookListBean bookListBean) {
        String[] menus;
        menus = mBookCommunityFragment.getResources().getStringArray(R.array.menu_long_comment_click);
        AlertDialog commentBeanDialog = new AlertDialog.Builder( mBookCommunityFragment.getContext())
                .setTitle(bookListBean.getTitle())
                .setAdapter(new ArrayAdapter<String>( mBookCommunityFragment.getContext(),
                                android.R.layout.simple_list_item_1, menus),
                        (dialog, which) -> onItemMenuClick(menus[which], bookListBean))
                .setNegativeButton(null, null)
                .setPositiveButton(null, null)
                .create();

        commentBeanDialog.show();
    }

    private void onMyListItemMenuClick(String which, BookListBean bookListBean) {
        switch (which) {
            //点赞
            case "点赞":
                int likeCount = bookListBean.getCollectorCount()+1;
                bookListBean.setCollectorCount(likeCount);
                mBookListService.updateEntity(bookListBean);
                TextHelper.showText("点赞成功");
                initAdapter();

                break;
            case "发布":
                if(bookListBean.getUsername().equals(userBean.getUsername())){
                    if(bookListBean.getIsRelease()){
                        TextHelper.showText("书单已经发布！");
                    }else {
                        bookListBean.setIsRelease(true);
                        mBookListService.updateEntity(bookListBean);
                        TextHelper.showText("发布成功！");
                    }
                }

                break;
            case "取消发布":
                if(bookListBean.getUsername().equals(userBean.getUsername())){
                    if(bookListBean.getIsRelease()){
                        bookListBean.setIsRelease(false);
                        mBookListService.updateEntity(bookListBean);
                        TextHelper.showText("取消发布成功！");
                    }else {
                        TextHelper.showText("书单并没有发布！");
                    }
                }

                break;
            //删除
            case "删除":
                mBookListBean = bookListBean;
                mHandle.sendMessage(mHandle.obtainMessage(1));

                break;
            default:
                break;
        }
    }

    private void onItemMenuClick(String which, BookListBean bookListBean) {
        switch (which) {
            //点赞
            case "点赞":
                int likeCount = bookListBean.getCollectorCount()+1;
                bookListBean.setCollectorCount(likeCount);
                mBookListService.updateEntity(bookListBean);
                TextHelper.showText("点赞成功");
                initAdapter();

                break;
            //删除
            case "删除":
                mBookListBean = bookListBean;
                mHandle.sendMessage(mHandle.obtainMessage(1));

                break;
            default:
                break;
        }
    }
    public void initAdapter() {
        mybookLists = mBookListService.getMyBookLists(userBean);
        subbookLists = mBookListService.getSubBookLists(userBean);
        mBookListCreateAdapter.refreshItems(mybookLists);
        mBookListSubscribeAdapter.refreshItems(subbookLists);
    }

}
